package com.buaa.student.post.MyList;



import com.alibaba.fastjson.JSON;
import com.buaa.student.post.to.Lists;
import com.buaa.student.post.to.ResponseStuTo;
import com.buaa.student.post.vo.LastTimeDakaInfo;
import com.buaa.student.post.vo.OldInfo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;


public class StepTwoUtils {


    public Boolean sendDakaPost(Lists student) throws Exception {

        CloseableHttpClient httpClient = addCookieHttpClient("ugmhg5iooruhuaijdkin3fho51");

        //"https://app.buaa.edu.cn/buaaxsncov/wap/datacube-base/ulists?date=2021-05-15&type=weishangbao&page=1&page_size=2000&group_id=1&group_type=1&keywords=";
        //创建URLBuilder
        URIBuilder uriBuilder = new URIBuilder("https://app.buaa.edu.cn/buaaxsncov/wap/default/save");

        //2.创建HttpGet对象，设置URL地址
        URI uri = uriBuilder.build();
        HttpPost httpPost = new HttpPost(uri);


        //3.设置代理和防盗链，以免被系统发现
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36");
        httpPost.addHeader("Referer", "https://app.buaa.edu.cn/site/buaaStudentNcov/index?xgh=" + student.getXgh() + "&group_id=1");
        //4.设置请求类型
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

        //5.加上请求体的参数之后的post请求
        HttpPost newHttpPost = addPostParams(httpPost, student);

        //6.执行Post请求
        CloseableHttpResponse response = httpClient.execute(newHttpPost);

        //7.获取结果实体
        HttpEntity entity = response.getEntity();
        String body = "";
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity);
            System.out.println("post打卡请求的响应结果："+body);
        }
        //8.释放链接
        response.close();

        return true;
    }

    private OldInfo getLastTimeDakaInfo(Lists student) throws URISyntaxException {

        CloseableHttpClient httpClient = addCookieHttpClient("dalk4r4rlomndh5rr5d15cbkn2");

        //创建URLBuilder
        URIBuilder uriBuilder = new URIBuilder("https://app.buaa.edu.cn/buaaxsncov/wap/default/get-info");
        //设置参数1，学号为该学生的学号
        uriBuilder.setParameter("xgh", student.getXgh());
        //设置参数2，group_id为1
        uriBuilder.setParameter("group_id", "1");

        //2.创建HttpGet对象，设置URL地址
        URI uri = uriBuilder.build();
        HttpGet httpGet = new HttpGet(uri);

        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36");

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //响应成功
                String content = EntityUtils.toString(response.getEntity(), "utf8");

                LastTimeDakaInfo lastTimeDakaInfo = JSON.parseObject(content, LastTimeDakaInfo.class);
                OldInfo oldInfo = lastTimeDakaInfo.getD().getOldInfo();

                return oldInfo;

            } else {
                System.out.println("响应失败，响应码为:" + response.getStatusLine().getStatusCode());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }

    /**
     * 添加打卡参数，涉及到位置信息
     *
     * @param httpPost
     * @param student
     * @return
     */
    private HttpPost addPostParams(HttpPost httpPost, Lists student) throws Exception {

        OldInfo oldInfo = getLastTimeDakaInfo(student);
        System.out.println(oldInfo);

        List<NameValuePair> parameters = new ArrayList<>();

        Class<?> clazz = oldInfo.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            parameters.add(new BasicNameValuePair(field.getName(),field.get(oldInfo).toString()));
        }

        parameters.add(new BasicNameValuePair("realname",student.getRealname()));
        parameters.add(new BasicNameValuePair("number",student.getXgh()));
        parameters.add(new BasicNameValuePair("gwszdd",""));
        parameters.add(new BasicNameValuePair("created_uid","300068"));
        parameters.add(new BasicNameValuePair("xgh",student.getXgh()));
        parameters.add(new BasicNameValuePair("group_id","1"));


//        //sfzs=1
//        parameters.add(new BasicNameValuePair("sfzs","1"));
//        //&bzxyy=
//        parameters.add(new BasicNameValuePair("bzxyy",""));
//        //&bzxyy_other=
//        parameters.add(new BasicNameValuePair("bzxyy_other",""));
//        //&brsfzc=1
//        parameters.add(new BasicNameValuePair("brsfzc","1"));
//        //&tw=
//        parameters.add(new BasicNameValuePair("tw",""));
//        //&sfcxzz=
//        parameters.add(new BasicNameValuePair("sfcxzz",""));
//        //&zdjg=
//        parameters.add(new BasicNameValuePair("zdjg",""));
//        //&zdjg_other=
//        parameters.add(new BasicNameValuePair("zdjg_other",""));
//        //&sfgl=
//        parameters.add(new BasicNameValuePair("sfgl",""));
//        //&gldd=
//        parameters.add(new BasicNameValuePair("gldd",""));
//        //&gldd_other=
//        parameters.add(new BasicNameValuePair("gldd_other",""));
//        //&glyy=
//        parameters.add(new BasicNameValuePair("glyy",""));
//        //&glyy_other=
//        parameters.add(new BasicNameValuePair("glyy_other",""));
//        //&gl_start=
//        parameters.add(new BasicNameValuePair("gl_start",""));
//        //&gl_end=
//        parameters.add(new BasicNameValuePair("gl_end",""));
//        //&sfmqjc=
//        parameters.add(new BasicNameValuePair("sfmqjc",""));
//        //&sfzc_14=1
//        parameters.add(new BasicNameValuePair("sfzc_14","1"));
//        //&sfqw_14=0
//        parameters.add(new BasicNameValuePair("sfqw_14","0"));
//        //&sfqw_14_remark=
//        parameters.add(new BasicNameValuePair("sfqw_14_remark",""));
//        //&sfzgfx=0
//        parameters.add(new BasicNameValuePair("sfzgfx","0"));
//        //&sfzgfx_remark=
//        parameters.add(new BasicNameValuePair("sfzgfx_remark",""));
//        //&sfjc_14=0
//        parameters.add(new BasicNameValuePair("sfjc_14","0"));
//        //&sfjc_14_remark=
//        parameters.add(new BasicNameValuePair("sfjc_14_remark",""));
//        //&sfjcqz_14=0
//        parameters.add(new BasicNameValuePair("sfjcqz_14","0"));
//        //&sfjcqz_14_remark=
//        parameters.add(new BasicNameValuePair("sfjcqz_14_remark",""));
//        //&sfgtjz_14=0
//        parameters.add(new BasicNameValuePair("sfgtjz_14","0"));
//        //&sfgtjz_14_remark=
//        parameters.add(new BasicNameValuePair("sfgtjz_14_remark",""));
//        //&szsqqz=0
//        parameters.add(new BasicNameValuePair("szsqqz","0"));
//        //&sfyqk=
//        parameters.add(new BasicNameValuePair("sfyqk",""));
//        //&szdd=1
//        parameters.add(new BasicNameValuePair("szdd","1"));
//        //&area=%E5%8C%97%E4%BA%AC%E5%B8%82%20%E5%8C%97%E4%BA%AC%E5%B8%82%20%E6%B5%B7%E6%B7%80%E5%8C%BA
//        parameters.add(new BasicNameValuePair("area","%E5%8C%97%E4%BA%AC%E5%B8%82%20%E5%8C%97%E4%BA%AC%E5%B8%82%20%E6%B5%B7%E6%B7%80%E5%8C%BA"));
//        //&city=%E5%8C%97%E4%BA%AC%E5%B8%82
//        parameters.add(new BasicNameValuePair("city","%E5%8C%97%E4%BA%AC%E5%B8%82"));
//        //&province=%E5%8C%97%E4%BA%AC%E5%B8%82
//        parameters.add(new BasicNameValuePair("province","%E5%8C%97%E4%BA%AC%E5%B8%82"));
//        //&address=%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E8%8A%B1%E5%9B%AD%E8%B7%AF%E8%A1%97%E9%81%93%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%AD%A6%E9%99%A2%E8%B7%AF%E6%A0%A1%E5%8C%BA
//        parameters.add(new BasicNameValuePair("address","%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E8%8A%B1%E5%9B%AD%E8%B7%AF%E8%A1%97%E9%81%93%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%AD%A6%E9%99%A2%E8%B7%AF%E6%A0%A1%E5%8C%BA\n"));
//        //&geo_api_info=%7B%22type%22%3A%22complete%22%2C%22position%22%3A%7B%22Q%22%3A39.980918240018%2C%22R%22%3A116.35088840060803%2C%22lng%22%3A116.350888%2C%22lat%22%3A39.980918%7D%2C%22location_type%22%3A%22html5%22%2C%22message%22%3A%22Get%20geolocation%20success.Convert%20Success.Get%20address%20success.%22%2C%22accuracy%22%3A29%2C%22isConverted%22%3Atrue%2C%22status%22%3A1%2C%22addressComponent%22%3A%7B%22citycode%22%3A%22010%22%2C%22adcode%22%3A%22110108%22%2C%22businessAreas%22%3A%5B%7B%22name%22%3A%22%E4%BA%94%E9%81%93%E5%8F%A3%22%2C%22id%22%3A%22110108%22%2C%22location%22%3A%7B%22Q%22%3A39.99118%2C%22R%22%3A116.34157800000003%2C%22lng%22%3A116.341578%2C%22lat%22%3A39.99118%7D%7D%2C%7B%22name%22%3A%22%E7%89%A1%E4%B8%B9%E5%9B%AD%22%2C%22id%22%3A%22110108%22%2C%22location%22%3A%7B%22Q%22%3A39.977965%2C%22R%22%3A116.37172700000002%2C%22lng%22%3A116.371727%2C%22lat%22%3A39.977965%7D%7D%5D%2C%22neighborhoodType%22%3A%22%E7%94%9F%E6%B4%BB%E6%9C%8D%E5%8A%A1%3B%E7%94%9F%E6%B4%BB%E6%9C%8D%E5%8A%A1%E5%9C%BA%E6%89%80%3B%E7%94%9F%E6%B4%BB%E6%9C%8D%E5%8A%A1%E5%9C%BA%E6%89%80%22%2C%22neighborhood%22%3A%22%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%22%2C%22building%22%3A%22%22%2C%22buildingType%22%3A%22%22%2C%22street%22%3A%22%E5%AD%A6%E9%99%A2%E8%B7%AF%22%2C%22streetNumber%22%3A%2237%E5%8F%B7%22%2C%22country%22%3A%22%E4%B8%AD%E5%9B%BD%22%2C%22province%22%3A%22%E5%8C%97%E4%BA%AC%E5%B8%82%22%2C%22city%22%3A%22%22%2C%22district%22%3A%22%E6%B5%B7%E6%B7%80%E5%8C%BA%22%2C%22township%22%3A%22%E8%8A%B1%E5%9B%AD%E8%B7%AF%E8%A1%97%E9%81%93%22%7D%2C%22formattedAddress%22%3A%22%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E8%8A%B1%E5%9B%AD%E8%B7%AF%E8%A1%97%E9%81%93%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%AD%A6%E9%99%A2%E8%B7%AF%E6%A0%A1%E5%8C%BA%22%2C%22roads%22%3A%5B%5D%2C%22crosses%22%3A%5B%5D%2C%22pois%22%3A%5B%5D%2C%22info%22%3A%22SUCCESS%22%7D
//        parameters.add(new BasicNameValuePair("geo_api_info","%7B%22type%22%3A%22complete%22%2C%22position%22%3A%7B%22Q%22%3A39.980918240018%2C%22R%22%3A116.35088840060803%2C%22lng%22%3A116.350888%2C%22lat%22%3A39.980918%7D%2C%22location_type%22%3A%22html5%22%2C%22message%22%3A%22Get%20geolocation%20success.Convert%20Success.Get%20address%20success.%22%2C%22accuracy%22%3A29%2C%22isConverted%22%3Atrue%2C%22status%22%3A1%2C%22addressComponent%22%3A%7B%22citycode%22%3A%22010%22%2C%22adcode%22%3A%22110108%22%2C%22businessAreas%22%3A%5B%7B%22name%22%3A%22%E4%BA%94%E9%81%93%E5%8F%A3%22%2C%22id%22%3A%22110108%22%2C%22location%22%3A%7B%22Q%22%3A39.99118%2C%22R%22%3A116.34157800000003%2C%22lng%22%3A116.341578%2C%22lat%22%3A39.99118%7D%7D%2C%7B%22name%22%3A%22%E7%89%A1%E4%B8%B9%E5%9B%AD%22%2C%22id%22%3A%22110108%22%2C%22location%22%3A%7B%22Q%22%3A39.977965%2C%22R%22%3A116.37172700000002%2C%22lng%22%3A116.371727%2C%22lat%22%3A39.977965%7D%7D%5D%2C%22neighborhoodType%22%3A%22%E7%94%9F%E6%B4%BB%E6%9C%8D%E5%8A%A1%3B%E7%94%9F%E6%B4%BB%E6%9C%8D%E5%8A%A1%E5%9C%BA%E6%89%80%3B%E7%94%9F%E6%B4%BB%E6%9C%8D%E5%8A%A1%E5%9C%BA%E6%89%80%22%2C%22neighborhood%22%3A%22%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%22%2C%22building%22%3A%22%22%2C%22buildingType%22%3A%22%22%2C%22street%22%3A%22%E5%AD%A6%E9%99%A2%E8%B7%AF%22%2C%22streetNumber%22%3A%2237%E5%8F%B7%22%2C%22country%22%3A%22%E4%B8%AD%E5%9B%BD%22%2C%22province%22%3A%22%E5%8C%97%E4%BA%AC%E5%B8%82%22%2C%22city%22%3A%22%22%2C%22district%22%3A%22%E6%B5%B7%E6%B7%80%E5%8C%BA%22%2C%22township%22%3A%22%E8%8A%B1%E5%9B%AD%E8%B7%AF%E8%A1%97%E9%81%93%22%7D%2C%22formattedAddress%22%3A%22%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E8%8A%B1%E5%9B%AD%E8%B7%AF%E8%A1%97%E9%81%93%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%8C%97%E4%BA%AC%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9%E5%A4%A7%E5%AD%A6%E5%AD%A6%E9%99%A2%E8%B7%AF%E6%A0%A1%E5%8C%BA%22%2C%22roads%22%3A%5B%5D%2C%22crosses%22%3A%5B%5D%2C%22pois%22%3A%5B%5D%2C%22info%22%3A%22SUCCESS%22%7D\n"));
//        //&gwdz=
//        parameters.add(new BasicNameValuePair("gwdz",""));
//        //&is_move=0
//        parameters.add(new BasicNameValuePair("is_move","0"));
//        //&move_reason=
//        parameters.add(new BasicNameValuePair("move_reason",""));
//        //&move_remark=
//        parameters.add(new BasicNameValuePair("move_remark",""));
//        //&realname=%E6%88%B4%E4%B8%B9%E9%9D%92
//        parameters.add(new BasicNameValuePair("realname","%E6%88%B4%E4%B8%B9%E9%9D%92"));
//        //&number=ZY1906809
//        //TODO 1.注意，这里是填学号
//        parameters.add(new BasicNameValuePair("number",student.getXgh().toString()));
//        //&uid=377026
//        parameters.add(new BasicNameValuePair("uid","377026"));
//        //&created=1620901044
//        //TODO 2.注意，这里是前一天的时间戳
//        parameters.add(new BasicNameValuePair("created", Long.toString(System.currentTimeMillis()-86400*1000L)));
//        //&date=20210513
//        //TODO 3.注意，这里是前一天的日期
//        String dateStr = new Date(System.currentTimeMillis() - 86400*1000L).toString().replace("-", "");
//        parameters.add(new BasicNameValuePair("date", dateStr));
//        //&id=2097077
//        parameters.add(new BasicNameValuePair("id","2097077"));
//        //&gwszdd=
//        parameters.add(new BasicNameValuePair("gwszdd",""));
//        //&created_uid=300068
//        parameters.add(new BasicNameValuePair("created_uid","300068"));
//        //&xgh=ZY1906809
//        //TODO 4.注意，这里是学号
//        parameters.add(new BasicNameValuePair("xgh",student.getXgh()));
//        //&group_id=1
//        parameters.add(new BasicNameValuePair("group_id","1"));

        UrlEncodedFormEntity formEntity = null;

        try {
            formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpPost.setEntity(formEntity);

        return httpPost;
    }

    /**
     * 返回包含必备的Cookie值的HttpClient
     *
     * @param eaiValue 已登录状态的凭证
     * @return
     */
    private CloseableHttpClient addCookieHttpClient(String eaiValue) {

        BasicCookieStore cookieStore = new BasicCookieStore();

        //设置eai-sess
        BasicClientCookie cookieEai = new BasicClientCookie("eai-sess", eaiValue);
        cookieEai.setVersion(0);
        cookieEai.setDomain("app.buaa.edu.cn");
        cookieEai.setPath("/");
        cookieStore.addCookie(cookieEai);

        //设置UUkey
        BasicClientCookie cookieUUkey = new BasicClientCookie("UUkey", "6d9f8a1dd6e0322c86e22b8ab6a8df3c");
        cookieUUkey.setVersion(0);
        cookieUUkey.setDomain("app.buaa.edu.cn");
        cookieUUkey.setPath("/");
        cookieStore.addCookie(cookieUUkey);

        //设置xxbh21.8
        BasicClientCookie cookieXxbh = new BasicClientCookie("xxbh21.8", "94424103");
        cookieXxbh.setVersion(0);
        cookieXxbh.setDomain("app.buaa.edu.cn");
        cookieXxbh.setPath("/");
        cookieStore.addCookie(cookieXxbh);

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        return httpClientBuilder.setDefaultCookieStore(cookieStore).build();
    }
}
